package com.nativetalk.service.four.index;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.category.TsNtCategoryIndex;
import com.nativetalk.bean.category.TsNtCategoryIndexNew;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherList;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.*;
import com.nativetalk.dto.TeacherRecommendResponse;
import com.nativetalk.dto.UpcomingResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/10/27.
 */
@Service
public class FourIndexServiceImpl implements FourIndexService {
    private static Logger logger = Logger.getLogger(FourIndexServiceImpl.class);
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;
    @Resource
    private TdNtReserveDao tdNtReserveDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;


    @Override
    public RetInfo index(Integer memb_id) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * banner列表(图片,链接,标题)
             */
            Map<String, Object> map = new HashMap<>();
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            map.put("waijiaojunIntroVideoUrl", "http://file.waijiaojun.com/06611d8e-ae4b-4f6d-a7f2-332637d1bb4d-3001-1489051469991-shipin1.mp4");
            List<TdNtBannerInfoIndex> tdNtBannerInfoIndices = (List<TdNtBannerInfoIndex>) MemcachedUtils.get(MemcachedKey.bannerInfoAPPIndexs);
            map.put("banner", tdNtBannerInfoIndices);
            System.out.println("fdafafa");
            /**
             * 课程类别(id,图标,名称)
             */
            List<TsNtCategoryIndex> tsNtCategoryIndices = (List<TsNtCategoryIndex>) MemcachedUtils.get(MemcachedKey.tsNtCategoryIndices);
            List<TsNtCategoryIndexNew> tsNtCategoryIndexNews = new ArrayList<>();
            for (int i = 0; i < tsNtCategoryIndices.size(); i++) {
                TsNtCategoryIndexNew tsNtCategoryIndexNew = new TsNtCategoryIndexNew();
                tsNtCategoryIndexNew.setCate_id(tsNtCategoryIndices.get(i).getCate_id());
                tsNtCategoryIndexNew.setCate_img(tsNtCategoryIndices.get(i).getCate_img());
                tsNtCategoryIndexNew.setCate_name(tsNtCategoryIndices.get(i).getCate_name());
                tsNtCategoryIndexNews.add(i, tsNtCategoryIndexNew);
            }
            map.put("category", tsNtCategoryIndexNews);
            System.out.println("fdafafa");

            /**
             * 推荐老师列表(头像,状态,id,名称,学校,关注人数,教学时长,系统标签,类别标签)
             */
            Page page = new Page();
            page.setCurrentPage(0);
            page.setPageSize(10);
            page.setObj(0);
            List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.findTdNtTeacherList(page);
            for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                if (tdNtTeacherLists.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                }
                String country = tdNtTeacherLists.get(i).getNationality();
                if (country.equals("0") || country.equals("315")) {
                    country = "1";
                }
                String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                tdNtTeacherLists.get(i).setNationality(nationality);
                /**
                 * 查找老师粉丝数
                 */
                int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                tdNtTeacherLists.get(i).setFans_size(funsnumber);
//                /**
//                 * 查找是否收藏老师 1未收藏0已收藏
//                 */
                String collection_flag = "1";
                tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
                /**
                 * 查找老师系统标签
                 */
                Map<String, Object> params = new HashMap<>();
                params.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                params.put("lab_type", 1);
                tdNtTeacherLists.get(i).setLabels(tdNtTeacherLabelDao.findTdNtTeacherListTeaIdAndType(params));
            }
            //查找用户有没有预约过老师
            Boolean trialable = false;
            Integer trail_time = 0;
            if (memb_id > 0) {
                trail_time = tdNtMemberDao.findUserBuyCourse(memb_id);
            }
            if (trail_time > 0 || memb_id == 0) {
                trialable = true;
            }
            map.put("trialable", trialable);
            map.put("teachers", tdNtTeacherLists);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public RetInfo appointmentUpcoming(TdNtMember tdNtMember) {
        RetInfo retInfo = RetInfo.getSuccessInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            // 1v1 预约课
            UpcomingResponse upcomingResponse1 = tdNtReserveDao.findUpcomingByMembId(tdNtMember.getMemb_id());
            if (upcomingResponse1 != null) {
                upcomingResponse1.setCourseType(1);
            }
            Boolean trialable = false;
            Integer trail_time = 0;
            trail_time = tdNtMemberDao.findUserBuyCourse(tdNtMember.getMemb_id());
            if (trail_time > 0) {
                trialable = true;
            }

            // 1vn 系统班课
            UpcomingResponse upcomingResponse2 = tdNtReserveDao.findMcCourseMembId(tdNtMember.getMemb_id());
            if (upcomingResponse2 != null) {
                upcomingResponse2.setCourseType(2);
            }

            UpcomingResponse upcomingResponse = new UpcomingResponse();
            // 两种类型的课程都有，选择最近要上课的课程
            if (have_1v1(upcomingResponse1) && have_1vn(upcomingResponse2)) {
                if (upcomingResponse1.getStartTime().getTime() <= upcomingResponse2.getStartTime().getTime()) {
                    upcomingResponse = upcomingResponse1;
                }else{
                    upcomingResponse = upcomingResponse2;
                }
            }else if(have_1v1(upcomingResponse1)){
                upcomingResponse = upcomingResponse1;
            }else if(have_1vn(upcomingResponse2)){
                upcomingResponse = upcomingResponse2;
            }else{
                retInfo.setTip("无预约课");
            }
            map.put("upcomingResponse", upcomingResponse);
            map.put("trialable", trialable);// 是否使用过体验券
            retInfo.setObj(map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            retInfo = RetInfo.getServerErrorInfo(Lang.ZH);
        }
        return retInfo;
    }

    private boolean have_1v1(UpcomingResponse o) {
        return o != null && o.getReserverId() != null && o.getReserverId().intValue() != 0;
    }

    private boolean have_1vn(UpcomingResponse o) {
        return o != null && o.getRoomId() != null && o.getRoomId().intValue() != 0;
    }

    public RetInfo teacherRecommend() {
        RetInfo retInfo = RetInfo.getSuccessInfo();
        List<TeacherRecommendResponse> list = tdNtTeacherDao.findHotTeachers();
        retInfo.setObj(list);
        return retInfo;
    }

}
